package net.jarang.android.saleoffline.activity;

import net.jarang.android.saleoffline.SaleContext;
import net.jarang.android.saleoffline.domain.appinfo.AppManager;
import net.jarang.android.saleoffline.domain.appinfo.Member;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MemberRegisterActivity extends Activity {
	private EditText myPhoneNoET;
	private EditText passwordNo1ET;
	private EditText passwordNo2ET;
	private EditText nickNameET;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_register);
        setTitle("회원 가입");
        
    	myPhoneNoET = (EditText)findViewById(R.id.myPhoneNoET);
    	myPhoneNoET.setText(SaleContext.myPhoneNo);
    	
    	passwordNo1ET = (EditText)findViewById(R.id.passwordNo1ET);
    	passwordNo2ET = (EditText)findViewById(R.id.passwordNo2ET);
    	nickNameET = (EditText)findViewById(R.id.nickNameET);
	}
	
	public void onClickMemberRegister(View v) {
		if (!checkValue()) {
			return;
		}
		
		String phoneNo = myPhoneNoET.getText().toString();
		String nickname = nickNameET.getText().toString();
		String password = passwordNo1ET.getText().toString();
		new AppManager(this).insert(new Member(phoneNo, nickname, password, true, true));

		Intent intent = new Intent(this, MemberViewActivity.class);
		startActivity(intent);
    	finish();
	}

	private boolean checkValue() {
		if (passwordNo1ET.getText() == null || passwordNo1ET.getText().length() < 1) {
			new AlertDialog.Builder(this)
			.setMessage("비밀번호를 입력하세요.")
	    	.setNegativeButton("확인", null)
	    	.show();
			return false;
		}
		
		if (!(passwordNo1ET.getText().toString().equals(passwordNo2ET.getText().toString()))) {
			new AlertDialog.Builder(this)
			.setMessage("비밀번호가 일치하지 않습니다.")
	    	.setNegativeButton("확인", null)
	    	.show();
			return false;
		}
		
		if (nickNameET.getText() == null || nickNameET.getText().length() < 1) {
			new AlertDialog.Builder(this)
			.setMessage("닉네임을 입력하세요.")
	    	.setNegativeButton("확인", null)
	    	.show();
			return false;
		}
		return true;
	}
	
    @Override
    public void onBackPressed() {
    	Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
    	finish();
    }	
}
